# -*- coding: utf8 -*-

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import login_required
from yamw.models import *
from datetime import date

class HomePage(webapp.RequestHandler):
    """ 主页 """
    
    def get(self):
        self.redirect('/task')
    
class LogoutPage(webapp.RequestHandler):
    """ 退出 """
    
    def get(self):
        self.redirect(users.create_logout_url('/task'))
        
class ResetPage(webapp.RequestHandler):
    """ 重置数据库环境 """
    
    @login_required
    def get(self):
        user = users.get_current_user()

        spaces = Space.all().filter('user =', user).fetch(1000)
        for space in spaces:
            space.delete()
        
        tasks = Task.all().filter('user =', user).fetch(1000)
        for task in tasks:
            task.delete()
            
        groups = TaskGroup.all().filter('user =', user).fetch(1000)
        for group in groups:
            group.delete()
                        
        def init_data():
            us = Space(user=user)
            us.put()
            
            group1 = TaskGroup(parent=us, user=user, name="Task Group1", create_date=date(2008,7,1))
            group1.put()
            group2 = TaskGroup(parent=us, user=user, name="Task Group2", create_date=date(2008,7,4))
            group2.put()
            TaskGroup(parent=us, user=user, name="Task Group3", create_date=date(2008,7,3)).put()
            
            Task(parent=us, user=user,name="I want do it 1", group=group1, create_date=date(2008,7,2)).put()
            Task(parent=us, user=user,name="I want do it 2", group=group1, create_date=date(2008,7,8)).put()
            Task(parent=us, user=user,name="I want do it 3", create_date=date(2008,6,30)).put()
            Task(parent=us, user=user,name="I want do it 4", create_date=date(2008,7,3)).put()
            Task(parent=us, user=user,name="I want do it 5", create_date=date(2008,7,4)).put()
            Task(parent=us, user=user,name="I want do it 6", group=group2, create_date=date(2008,7,4)).put()
            Task(parent=us, user=user,name="I want do it 7", group=group2, create_date=date(2008,7,4)).put()
            Task(parent=us, user=user,name="I want do it 8", create_date=date(2008,7,8)).put()
            Task(parent=us, user=user,name="I want do it 9", create_date=date(2008,7,6)).put()
            
        db.run_in_transaction(init_data)
        
        self.redirect('/task')
        
        